import os
import sqlite3
from pathlib import Path
import argparse
import shutil

def check_and_connect_db(db_path):
    """检查并连接到SQLite数据库"""
    conn = sqlite3.connect(db_path)
    # 设置sqlite3.Row作为行工厂，以便能够使用字典访问字段
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()
    return conn, cursor

def fetch_zip_records(conn, cursor):
    """从zip_hash表中获取所有记录"""
    cursor.execute('SELECT * FROM zip_hash')
    records = cursor.fetchall()
    return records

def copy_missing_files(records, scan_dir, base_dir):
    """根据zip_hash表中的记录，检查并复制缺失的ZIP文件到变更目录，同时保持文件夹结构"""
    scan_dir_path = Path(scan_dir)

    for record in records:
        file_path = record['file_path']
        file_name = record['file_name']
        source_path = Path(base_dir) / file_path / file_name
        target_path = scan_dir_path / file_path / file_name

        if not target_path.exists():
            target_path.parent.mkdir(parents=True, exist_ok=True)  # 确保目标目录存在
            try:
                shutil.copy(str(source_path), str(target_path))
                print(f"已复制文件: {source_path} 到 {target_path}")
            except FileNotFoundError:
                print(f"警告: 源文件 {source_path} 不存在")
            except Exception as e:
                print(f"处理文件 {source_path} 时出错: {e}")
        else:
            print(f"文件已存在: {target_path}")

def main(db_directory, scan_dir):
    db_path = str(Path(db_directory) / 'file_hashes.db')
    conn, cursor = check_and_connect_db(db_path)

    records = fetch_zip_records(conn, cursor)
    copy_missing_files(records, scan_dir, db_directory)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="根据zip_hash表中的记录，检查并复制缺失的ZIP文件到变更目录，同时保持文件夹结构")
    parser.add_argument('--db', required=True, help="SQLite数据库存放位置")
    parser.add_argument('--scan-dir', required=True, help="变更目录")

    args = parser.parse_args()

    db_directory = args.db
    scan_dir = args.scan_dir

    main(db_directory, scan_dir)